/*
 * Copyright (c) 2009 LastFM.Desktop Anton Pavlik.
 * All Rights Reserved.
 * 
 * You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, 
 * with or without modifications, and in Source or Object form, provided that You meet the following 
 * conditions:
 * 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and
 *
 * 2. You must cause any modified files to carry prominent notices stating that You changed the files; 
 * and
 *
 * 3. You must retain, in the Source form of any Derivative Works that You distribute, all copyright, 
 * patent, trademark, and attribution notices from the Source form of the Work, excluding those notices 
 * that do not pertain to any part of the Derivative Works.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, Licensor provides the Work 
 * (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties 
 * or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. 
 * YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE WORK
 * AND ASSUME ANY RISKS ASSOCIATED WITH YOUR EXERCISE OF PERMISSIONS UNDER THIS LICENSE. 
 */
package org.shake.lastfm.ui.info.pages.track;

import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import org.bushe.swing.event.annotation.AnnotationProcessor;
import org.bushe.swing.event.annotation.EventTopicSubscriber;
import org.jdesktop.swingx.JXPanel;
import org.shake.lastfm.event.EventTopic;
import org.shake.lastfm.event.TrackSelectedEvent;
import org.shake.lastfm.model.Track;
import org.shake.lastfm.swing.RemoteImageLoader;
import org.shake.lastfm.swing.SwingJob;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class TrackInfoPanel extends JXPanel
{
    private static final long serialVersionUID = 9016750836225696857L;
    private final static String EMPTY_STR = "";

    private Track selectedTrack;
    
    private JLabel titleLabel;
    private JLabel creatorLabel;
    private JLabel albumLabel;
    private JLabel albumImageLabel;
    
    private RemoteImageLoader imageLoader;
    
    public TrackInfoPanel()
    {
        super();
        
        buildUI();
        AnnotationProcessor.process(this);
    }
    
    private void buildUI()
    {
        FormLayout layout = new FormLayout(
                "left:p, 3dlu, left:p",
                "top:p:grow, top:4dlu, top:p, top:1dlu, top:p"
        );
        setLayout(layout);
        CellConstraints cc = new CellConstraints();
        
        this.titleLabel = new JLabel();
        this.creatorLabel = new JLabel();
        this.albumLabel = new JLabel();
        this.albumImageLabel = new JLabel();
        Font originalTitleFont = this.titleLabel.getFont();
        Font titleFont = originalTitleFont.deriveFont(Font.BOLD, (float)(originalTitleFont.getSize() + 5));
        this.titleLabel.setFont(titleFont);

        add(this.albumImageLabel, cc.xywh(1, 1, 1, 5));
        add(this.titleLabel, cc.xy(3, 1));
        add(this.creatorLabel, cc.xy(3, 3));
        add(this.albumLabel, cc.xy(3, 5));
    }
    
    @EventTopicSubscriber(topic=EventTopic.TRACK_SELECTED)
    public void onTrackSelectionChanged(String topic, TrackSelectedEvent event)
    {
        this.selectedTrack = event.getTrack();
        updateLabels();
    }
    
    private void updateLabels()
    {
        this.titleLabel.setText(selectedTrack == null ? EMPTY_STR : selectedTrack.getTitle());
        this.creatorLabel.setText(selectedTrack == null ? EMPTY_STR : selectedTrack.getCreator());
        this.albumLabel.setText(selectedTrack == null ? EMPTY_STR : selectedTrack.getAlbum());

        this.albumImageLabel.setIcon(null);
        
        if (this.selectedTrack != null)
        {
            if (this.imageLoader != null && !this.imageLoader.isDone())
            {
                this.imageLoader.cancel(true);
            }
            
            this.imageLoader = new RemoteImageLoader(this.selectedTrack.getImage(), new LoadImageJob());
            this.imageLoader.execute();
        }
    }
    
    private class LoadImageJob implements SwingJob<ImageIcon>
    {
        @Override
        public void onJobCanceled(Exception ex)
        {
            TrackInfoPanel.this.albumImageLabel.setIcon(null);
        }

        @Override
        public void onJobDone(ImageIcon data)
        {
            TrackInfoPanel.this.albumImageLabel.setIcon(data);
        }

        @Override
        public ImageIcon readUI()
        {
            return null;
        }
    }

}